//
//  ViewController.m
//  TestForReactiveCocoa
//
//  Created by liangshangjia on 16/2/17.
//  Copyright © 2016年 Sami-Network. All rights reserved.
//

#import "ViewController.h"
#import "VCTableViewDelegate.h"
#import "VCViewModel.h"
@interface ViewController ()
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) VCTableViewDelegate *tableDelegate;
@property (nonatomic, strong) VCViewModel *viewModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configure];
}

- (void)configure
{
    //DataRecieve
    _viewModel = [[VCViewModel alloc]init];
    @weakify(self);
    [self.viewModel addObserverBlockForKeyPath:@"count" block:^(__weak id obj, id oldVal, id newVal) {
        @strongify(self);
        [self.tableView reloadData];
    }];
    
    
    //TableView
    _tableView = [[UITableView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    
    //UIBarButtonItem
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(refreshAction:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
    //UIDisplay
    _tableDelegate = [VCTableViewDelegate delegateWithViewModel:self.viewModel andTableView:self.tableView];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)refreshAction:(UIBarButtonItem *)sender {
    [self.viewModel request];
}



@end
